/*  
  -- The Lime II Project -- 

  A tuplespaces-based middleware for coordinating agents and mobile hosts.
  Copyright (C) 2005.
  Gian Pietro Picco, Amy L. Murphy, Lorenzo Bellini.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

package testing;

import lime2.*;
import lights.adapters.*;
import lights.interfaces.*;
import java.util.*;

/**
 * This example runs the LimeServer in diagnostics mode (port 8080): it
 * generates sample tuplespaces with tuples and reactions, so that you can
 * "look at" them from an Internet browser. <br>
 * Optional user input: tuplespace adapter to test.
 * 
 * @author Gian Pietro Picco
 * @author Amy Murphy
 * @author Lorenzo Bellini
 */

public class TestHTTP {
	// names (number) of the tuplespaces to use
	private static final String[] TUPLESPACES = new String[] { "app1", "app2",
			"app3", "app4" };

	// total number of tuples that will be put in the tuplespaces
	private static final int N_TUPLES = 50;

	// total number of reaction to install
	private static final int N_REACTIONS = 10;

	// sample tuple data (1)
	private static final String[] EXAMPLE_STRINGS1 = new String[] { "Aa", "Bb",
			"Dd", "Cc", "Ee" };

	// sample tuple data (2)
	private static final String[] EXAMPLE_STRINGS2 = new String[] { "HELLO",
			"SENT", "OK", "RCVD", "ACK", "HOLD", "BYE", "COMMIT", "UPDATE",
			"RST", };

	// sample reaction listeners
	class CleanBuffer implements ReactionListener {
		public void reactsToOut(ReactionEvent evt) {
		}

		@Override
		public void reactsToTake(ReactionEvent re) {
			// TODO Auto-generated method stub

		}
	};

	class SendAlert implements ReactionListener {
		public void reactsToOut(ReactionEvent evt) {
		}

		@Override
		public void reactsToTake(ReactionEvent re) {
			// TODO Auto-generated method stub

		}
	};

	class EnqueMsg implements ReactionListener {
		public void reactsToOut(ReactionEvent evt) {
		}

		@Override
		public void reactsToTake(ReactionEvent re) {
			// TODO Auto-generated method stub

		}
	};

	class WakeProcess implements ReactionListener {
		public void reactsToOut(ReactionEvent evt) {
		}

		@Override
		public void reactsToTake(ReactionEvent re) {
			// TODO Auto-generated method stub

		}
	};

	/**
	 * Main method.
	 * 
	 * @param args
	 *            user input: (optional) name of the tuplespace adapter to test
	 */
	public static void main(String[] args) {
		// prints msg
		System.out
				.println("--- open http://localhost:8080/ with your browser ---\n");

		// if the user did not choose any tuplespace factory then we use the
		// default tuplespace factory
		String tsfactory;
		if (args != null && args.length != 1)
			tsfactory = "lights.adapters.builtin.TupleSpaceFactory";
		else
			tsfactory = "lights.adapters." + args[0] + ".TupleSpaceFactory";

		// starts server
		Launcher.launch("-start -http 8080 -tsfactory " + tsfactory);

		// randomizer
		Random rnd = new Random();

		// creates tuplespaces
		LimeTupleSpace[] lt = new LimeTupleSpace[TUPLESPACES.length];
		for (int i = 0; i < lt.length; i++)
			lt[i] = LimeTupleSpace.get(TUPLESPACES[i]);

		// creates tuples
		ITuple[] t = new ITuple[N_TUPLES];
		for (int i = 0; i < t.length; i++) {
			t[i] = new Tuple();
			t[i].add(new Field().setValue(EXAMPLE_STRINGS1[rnd
					.nextInt(EXAMPLE_STRINGS1.length)]));
			t[i].add(new Field().setValue(EXAMPLE_STRINGS2[rnd
					.nextInt(EXAMPLE_STRINGS2.length)]));
			t[i].add(new Field().setValue(new Integer(rnd.nextInt(101))));
			t[i].add(new Field().setValue(new Boolean(rnd.nextBoolean())));
			t[i].add(new Field().setValue(new Integer(rnd.nextInt(101))));
		}

		// creates reactions
		Reaction[] r = new Reaction[N_REACTIONS];
		for (int i = 0; i < r.length; i++) {
			LimeTemplate ltemplate = new LimeTemplate(Location.HERE,
					Location.HERE, new Tuple().add(new Field()
							.setType(Integer.class)));
			ReactionListener ll = null;
			switch (rnd.nextInt(4)) {
			case 0:
				ll = (new TestHTTP()).new CleanBuffer();
				break;
			case 1:
				ll = (new TestHTTP()).new SendAlert();
				break;
			case 2:
				ll = (new TestHTTP()).new EnqueMsg();
				break;
			case 3:
				ll = (new TestHTTP()).new WakeProcess();
				break;
			}
			r[i] = new Reaction((rnd.nextInt(2) == 0 ? Reaction.ONCE
					: Reaction.ONCEPERTUPLE), ltemplate, ll);
		}

		// out of tuples
		for (int i = 0; i < t.length; i++)
			lt[rnd.nextInt(TUPLESPACES.length)].out(t[i]);

		// installs reactions
		for (int i = 0; i < r.length; i++)
			if (rnd.nextInt(2) == 0)
				lt[rnd.nextInt(TUPLESPACES.length)]
						.addStrongReaction(new Reaction[] { r[i] });
			else
				lt[rnd.nextInt(TUPLESPACES.length)]
						.addWeakReaction(new Reaction[] { r[i] });
	}

}
